package com.lingxu.module.collection.mapper;

import com.lingxu.module.collection.entity.ModelGuide;
import com.lingxu.module.collection.entity.ModelRunRecord;
import com.lingxu.module.collection.entity.ReportDetail;
import com.lingxu.module.collection.entity.ReportInfo;
import com.lingxu.module.redList.entity.RedList;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public interface ReportMapper {

    List<ReportInfo> getReportInfo(@Param("list") List<String> modelid);

    ModelRunRecord getModelRunRecordByModelId(@Param("modelid")String modelid);
    ModelRunRecord getModelRunRecordByModelIdAndRunId(@Param("modelid")String modelid, @Param("runid")String runid);
    Integer getResultCount(@Param("sql") String sql);

    List<LinkedHashMap<String, Object>> getResultList(@Param("sql") String sql);

    int getHmdAuthorityByUserId(String userid);

    List<RedList> getRedListAll();

    String findCountByRegionAndMonth(@Param("region") String region, @Param("yeardate") String yeardate);

    void updateReportNo(@Param("modelid") String modelid,@Param("reportno") String reportno);

    List<ModelGuide> findModelIssue(@Param("modelid") String modelid);

    int getTableNum(@Param("list") List<String> modelid);

    Map getDataFroms(String modelid);

    ReportDetail findModelDetail(String modelid);


    Map getDataFromsByModelId(String modelid);

    int getSum(String modelid);

    String getResultColumns(@Param("resultTable") String resultTable,@Param("modelid") String modelid);

    String getApprovestate(String id);

    void restReportNo();

    List<String> findModelJdgz(String mastermodelid);

    int findDataNumByModelId(String modelid);


    // 在 TModelMapper.java 中添加方法
    @Select({"<script>",
            "SELECT m.ID,m.NAME,m.TOPCATEGORY,m.SECONDCATEGORY,m.CREATORNAME,m.UPDATETIME,m.WORKKEY, m.REMARK, ",
            "(SELECT COUNT(*) FROM ALL_TABLES WHERE TABLE_NAME = CONCAT(r.RESULTTABLE, '_chack')) as hasCheckTable ",
            "FROM T_MODEL m ",
            "LEFT JOIN (",
            "  SELECT MODELID, RESULTTABLE, ENDTIME, ROW_NUMBER() OVER(PARTITION BY MODELID ORDER BY ENDTIME DESC) as rn ",
            "  FROM T_MODEL_RUN_RECORD ",
            "  WHERE RUNTYPE = '01' AND RUNSTATUS = '01' AND RUNPROCESS = '5'",
            ") r ON m.ID = r.MODELID AND r.rn = 1 ",
            "WHERE m.ISDELETE = 0 ",
            "AND m.TOPCATEGORY LIKE '%20%' ",
            "AND (SELECT COUNT(*) FROM ALL_TABLES WHERE TABLE_NAME = CONCAT(r.RESULTTABLE, '_chack')) = 1 ",
            "<if test='name != null and name != \"\"'>",
            "   AND m.NAME LIKE CONCAT('%', #{name}, '%')",
            "</if>",
            "<if test='topCategory != null and topCategory != \"\"'>",
            "   AND m.TOPCATEGORY = #{topCategory}",
            "</if>",
            "<if test='secondCategory != null and secondCategory != \"\"'>",
            "   AND m.SECONDCATEGORY = #{secondCategory}",
            "</if>",
            "ORDER BY m.ID ",
            "LIMIT #{pageSize} OFFSET #{offset}",
            "</script>"})
    List<Map<String, Object>> selectModelWithCheckTable(@Param("offset") long offset,
                                                        @Param("pageSize") long pageSize,
                                                        @Param("name") String name,
                                                        @Param("topCategory") String topCategory,
                                                        @Param("secondCategory") String secondCategory);

    @Select({"<script>",
            "SELECT COUNT(*) ",
            "FROM T_MODEL m ",
            "LEFT JOIN (",
            "  SELECT MODELID, RESULTTABLE, ENDTIME, ROW_NUMBER() OVER(PARTITION BY MODELID ORDER BY ENDTIME DESC) as rn ",
            "  FROM T_MODEL_RUN_RECORD ",
            "  WHERE RUNTYPE = '01' AND RUNSTATUS = '01' AND RUNPROCESS = '5'",
            ") r ON m.ID = r.MODELID AND r.rn = 1 ",
            "WHERE m.ISDELETE = 0 ",
            "AND m.TOPCATEGORY LIKE '%20%' ",
            "AND (SELECT COUNT(*) FROM ALL_TABLES WHERE TABLE_NAME = CONCAT(r.RESULTTABLE, '_chack')) = 1 ",
            "<if test='name != null and name != \"\"'>",
            "   AND m.NAME LIKE CONCAT('%', #{name}, '%')",
            "</if>",
            "<if test='topCategory != null and topCategory != \"\"'>",
            "   AND m.TOPCATEGORY = #{topCategory}",
            "</if>",
            "<if test='secondCategory != null and secondCategory != \"\"'>",
            "   AND m.SECONDCATEGORY = #{secondCategory}",
            "</if>",
            "</script>"})
    Long selectModelWithCheckTableCount(@Param("name") String name,
                                        @Param("topCategory") String topCategory,
                                        @Param("secondCategory") String secondCategory);

    // 在您的Mapper接口中添加：
    @Select("${sql}")
    Integer executeUpdateWithParams(@Param("sql") String sql, @Param("params") List<Object> params);
}
